<template>
	<view></view>
</template>
<script>
	import {
		PostPackage
	} from '@/api/all.js';
	var barcode = null;
	export default {
		data() {
			return {
				name: '222221', //要在扫码界面自定义的内容
				flash: false, //是否打开摄像头
				type: '',
				scansnum:0,//已扫描数
				affirm:0,//已确认数
				amount:0,//总金额

			};
		},
		onLoad(d) {
			
			var n = d.text;
			this.type = d.type;
			console.log(this.type,'type');
			// if (n) {
			// 	this.name = n;
			// }
			var pages = getCurrentPages();
			var page = pages[pages.length - 1];
			// #ifdef APP-PLUS
			plus.navigator.setFullscreen(true); //全屏
			var currentWebview = page.$getAppWebview();
			// console.log('currentWebview',JSON.stringify(currentWebview))
			this.createBarcode(currentWebview); //创建二维码窗口
			this.createView(currentWebview); //创建操作按钮及tips界面
			// #endif
			
		},
		
		methods: {
			// 提交订单
			Submission(result){
				let data ={
					packageNo:result,
					taskId:uni.getStorageSync('taskid')
				}
				console.log('传过去的taskid',data.taskId)
				PostPackage(data).then(res => {
					console.log('res==',res)
					let code = res.data.ErrorCode 
					this.scansnum=this.scansnum+1
					uni.setStorageSync('scansnum',this.scansnum);
					// console.log('this.scansnum',this.scansnum)
					if(code==200){
						this.ScanAudio();
						this.affirm=this.affirm+1
						uni.setStorageSync('affirm',this.affirm);
						this.amount=this.amount+res.data.data.orderMoney
						uni.setStorageSync('amount',this.amount);
						console.log('绘制一个√',code,'this.amount',this.amount)
						var scanEntry = new plus.nativeObj.View('scanEntry', {
								top: '70%',
								left: '25%',
								height: '10%',
								width: '50%'
						
							},
							[{
									tag: 'img',
									id: 'scanBar',
									src: 'static/images/Union.png',
									position: {
										width: '56',
										left: '36%',
										height: '56'
									}
								},
								,{
									tag: 'img',
									id: 'nextbtn',
									src: 'static/images/nextbtn.png',
									position: {
										width: '124',
										left: '15%',
										height: '44',
										top:'35%'
									}
								}
							],
						);
						var pages = getCurrentPages();
						var page = pages[pages.length - 1];
						var currentWebview = page.$getAppWebview();
						currentWebview.append(scanEntry);
						
					}else{
						this.ScanAudioerr();
						console.log('绘制一个X',code)
						var scanEntry = new plus.nativeObj.View('scanEntry', {
								top: '70%',
								left: '25%',
								height: '30%',
								width: '50%'
						
							},
							[{
									tag: 'img',
									id: 'scanBar',
									src: 'static/images/Unionerr.png',
									position: {
										width: '56',
										left: '36%',
										height: '56'
									}
								},{
									tag: 'img',
									id: 'nextbtn',
									src: 'static/images/nextbtn.png',
									position: {
										width: '124',
										left: '15%',
										height: '44',
										top:'35%'
									}
								},
								
							],
						);
						
						var pages = getCurrentPages();
						var page = pages[pages.length - 1];
						var currentWebview = page.$getAppWebview();
						// console.log('绘制一个X currentWebview',currentWebview)
						currentWebview.append(scanEntry);
						
					}
					scanEntry.interceptTouchEvent(true);
					scanEntry.addEventListener("click", function(e) {
						scanEntry.hide()
						setTimeout(() => {
											barcode.start()
											console.log('开始执行')
						 }, 1000)
						
					},false)
					
								}
							)
			},
			// 扫码成功回调
			onmarked(type, result) {
				var text = '未知: ';
				// console.log('内容' + JSON.stringify(result),type);
				this.Submission(JSON.stringify(result),type)
				
				// this.name.push(result)
				switch (type) {
					case plus.barcode.QR:
						text = 'QR: ';
						break;
					case plus.barcode.EAN13:
						text = 'EAN13: ';
						break;
					case plus.barcode.EAN8:
						text = 'EAN8: ';
						break;
				}
				// plus.navigator.setFullscreen(false);
				
				
				//兄弟传参
				// this.$eventHub.$emit(this.type, {
				// 	result: result
				// });				
				
				// uni.$emit(this.type, {
				// 	result: result
				// })
				// console.log(this.type,result,'this.type',text);
				// uni.navigateBack({
				// 	delta: 1
				// });
				// barcode.close();
				// 再次执行
				
			}, 
			// 创建二维码窗口
			createBarcode(currentWebview) {
				//自定义窗口大小
				//条码类型常量数组，默认情况支持QR、EAN13、EAN8类型。 通过此参数可设置扫码识别支持的条码类型（注意：设置支持的条码类型越多，扫描识别速度可能将会降低）
				barcode = plus.barcode.create('barcode', [plus.barcode.QR, plus.barcode.EAN13,plus.barcode.EAN8,plus.barcode.UPCA,plus.barcode.UPCE,plus.barcode.CODABAR,plus.barcode.CODE39,plus.barcode.CODE93,plus.barcode.CODE128,plus.barcode.ITF,plus.barcode.PDF417], {
					top: '0',
					left: '0',
					width: '100%',
					height: '100%',
					scanbarColor: '#1DA7FF',
					position: 'static',
					frameColor: '#1DA7FF'
				});
				barcode.onmarked = this.onmarked;
				barcode.setFlash(this.flash);
				currentWebview.append(barcode);
				barcode.start();
				
			},
			// 创建操作按钮及tips
			createView(currentWebview) {
				// 创建返回原生按钮
				var backVew = new plus.nativeObj.View('backVew', {
						top: '5px',
						left: '3px',
						height: '60px',
						width: '70%' 
					},
					[{ 
						tag: 'img',
						id: 'backBar',
						src: 'static/images/backBar.png',
						position: {
							top: '5px',
							left: '3px',
							width: '40px',
							height: '40px'
						}
					}]);
					
				// 创建打开手电筒的按钮
				var scanBarVew = new plus.nativeObj.View('scanBarVew', {
						top: '2%',
						left: '80%',
						height: '60px',
						width: '20%'

					},
					[{
							tag: 'img',
							id: 'scanBar',
							src: 'static/images/scanBar.png',
							position: {
								width: '40px',
								left: '36%',
								height: '40px'
							}
						}
					]);
					
					
				// 创建展示类内容组件
				var content = new plus.nativeObj.View('content', {
						top: '0px',
						left: '0px',
						height: '100%',
						width: '100%'

					},
					[{
							tag: 'font',
							id: 'scanTitle',
							text: '扫码',
							textStyles: {
								size: '24px',
								color: '#ffffff'
							},
							position: {
								top: '0px',
								left: '0px',
								width: '100%',
								height: '40px'
							}
						}

					]);
				backVew.interceptTouchEvent(true);
				scanBarVew.interceptTouchEvent(true);
				// scanEntry.interceptTouchEvent(true);
				currentWebview.append(content);
				// currentWebview.append(scanEntry);
				currentWebview.append(scanBarVew);
				currentWebview.append(backVew);
				backVew.addEventListener("click", function(e) { //返回按钮
				console.log('返回按钮')
					// uni.navigateBack({
					// 	delta: 1
					// });
					uni.navigateTo({
						url:"../createTask/createTask"
					})
					barcode.close();
					plus.navigator.setFullscreen(false);

				}, false);
				var temp = this;
				scanBarVew.addEventListener("click", function(e) { //点亮手电筒
				console.log('点亮手电筒')
					temp.flash = !temp.flash;
					if (temp.flash) {
						scanBarVew.draw([{
								tag: 'img',
								id: 'scanBar',
								src: 'static/images/yellow-scanBar.png',
								position: {
									width: '40px',
									left: '36%',
									height: '40px'
								}
							}
						]);
					} else {
						console.log('关闭手电筒')
						scanBarVew.draw([{
								tag: 'img',
								id: 'scanBar',
								src: 'static/images/scanBar.png',
								position: {
									width: '40px',
									left: '36%',
									height: '40px'
								}
							}
						])
					}
					if (barcode) {
						barcode.setFlash(temp.flash);
					}
				}, false)

			}
		},
		onBackPress() {
			// #ifdef APP-PLUS
			// 返回时退出全屏
			barcode.close();
			console.log("onBackPress");
			plus.navigator.setFullscreen(false);
			// #endif
		},
		onUnload() {
			
			console.log("onUnload");
			plus.navigator.setFullscreen(false);
		}


	};
</script>

<style scoped>
</style>
